package time.after.time.sys.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;

import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.type.JavaType;


/**
 * 
 * JAVA <---> JSON 转换辅助类
 * 
 * 杰克逊指南：开/关配置功能  http://wiki.fasterxml.com/JacksonOnOffFeatures
 * 
 * 维护单例Mapper对象 关闭配置功能  提供常用接口
 * 
 * 忽略包含unquotes控制字符（ASCII码32以下，包括标签，换行）[JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS]
 * 忽略遇到未知的属性[DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES]
 * 修改TIMESTAMP类型的日期默认处理  自定义输出格式(yyyy-MM-dd HH:mm:ss)
 * 
 * @author Aliyador in 2010-12-6
 * 
 */

public class Mapper {

	private static ObjectMapper m = new ObjectMapper();
	
	static{
		m.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		m.getDeserializationConfig().set(
				org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		
		m.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,false);
		m.getSerializationConfig().setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		
	}
	
	private static JsonFactory jf = new JsonFactory();

    /**
     * JSON 转换为 JAVA 对象
     * 
     * 示例: 
     * {
     *   MemcacheBean bean=Mapper.readValue(objJsonStr,MemcacheBean.class);
     * }
    */	
	public static <T> T readValue(String jsonAsString, Class<T> pojoClass)
			throws JsonMappingException, JsonParseException, IOException {
		return m.readValue(jsonAsString, pojoClass);
	}
	
    public static <T> T readValue(JsonNode root, Class<T> valueType)
        throws IOException, JsonParseException, JsonMappingException
    {
        return (T)m.readValue(root, valueType);
    } 
    
    /**
     * 序列化为集合类
     * 
     * 示例: 
     * {
     *  List<MemcacheBean> list=
     *    Mapper.readValue(listObjJsonStr, 
     *      TypeFactory.collectionType(ArrayList.class, MemcacheBean.class)
     *    );
     * }
    */
    @SuppressWarnings("unchecked")
    public static <T> T readValue(String content, JavaType valueType)
        throws IOException, JsonParseException, JsonMappingException
    {
        return (T)m.readValue(jf.createJsonParser(content), valueType);
    } 	
    
    @SuppressWarnings("unchecked")
    public static <T> T readValue(JsonNode root, JavaType valueType)
        throws IOException, JsonParseException, JsonMappingException
    {
        return (T) m.readValue(root, valueType);
    } 
    
    public static String writeValueAsString(Object value)
        throws IOException, JsonGenerationException, JsonMappingException
    {        
        return m.writeValueAsString(value);
    }
    /**
     * Object -> String 转换异常时 输出默认String
     */
    public static String writeValueAsString(Object value,String _default)
    {        
       try {
		return m.writeValueAsString(value);
	   } catch (Exception e) {
		return _default;
	   }
    }
    public static JsonNode readTree(String content)
              throws IOException, JsonProcessingException
    {
       return m.readTree(content);
    }
}